﻿using BuildStatus.Web.BuildServer;
using BuildStatus.Web.Models;
using BuildStatus.Web.Properties;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Web;
using System.Web.Http;

namespace BuildStatus.Web.Services
{
    [Route("api/embedded")]
    public class EmbeddedController : ApiController
    {
        // GET: api/values
        [HttpGet]
        [Route("api/embedded/{filename}/GetFile")]
        public HttpResponseMessage Get([FromUri]string filename)
        {
            filename = Path.GetFileName(filename); // Make sure there's no way the user can go outside the allowed folder...
            filename = Path.Combine(Path.GetDirectoryName(BuildSettings.Settings.EmbeddedHtmlFile), filename);

            var result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(File.ReadAllBytes(filename))
            };

            /*
            result.Content.Headers.ContentDisposition =
                new System.Net.Http.Headers.ContentDispositionHeaderValue("inline")
                {
                    FileName = Path.GetFileName(filename)
                };
            */
            result.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(filename));

            return result;
        }
    }
}